﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using Core.DAL;
using xManagerWebApp.__AdminCompany;

namespace xManagerWebApp.AdminCompany
{
    public partial class WF_DepartmentManagement : System.Web.UI.Page
    {

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            UpdateDepList();                
                
        }

        protected void SaveDepartment(object sender, EventArgs e)
        {
            if (this.tb_dep_name.Text != "")
            {
                Department d = new Department();
                d.Id = Guid.NewGuid();
                d.Name = this.tb_dep_name.Text;

                this.tb_dep_name.Text = "";

                using (DataAccessLayer dc = new DataAccessLayer())
                {
                    dc.DataContext.Departments.InsertOnSubmit(d);
                    dc.DataContext.SubmitChanges();
                    this.save_status.Text = "New department added!";                    
                }

                ListItem x = new ListItem(d.Name, d.Id.ToString());
                if (!this.drop_down_department_list.Items.Contains(x))
                    this.drop_down_department_list.Items.Add(x);

            }
        }

        protected void UpdateDepList()
        {
            using (DataAccessLayer dc = new DataAccessLayer())
            {
                var query = from t in dc.DataContext.Departments
                            select t;

                if (query.Count() != 0)
                {
                    foreach (Department d in query)
                        if (!IsPostBack) this.drop_down_department_list.Items.Add(new ListItem(d.Name, d.Id.ToString()));
                    
                    this.DepartmentList.DataSource = query;
                    this.DepartmentList.DataBind();
                }
                else this.DepartmentList.Visible = false;
            }            
            
            if (!IsPostBack) this.drop_down_department_list.SelectedIndex = 0;
            if(this.drop_down_department_list.Items.Count > 0)
                UpdateLevels();
        }

        protected void UpdateLevels()
        {
            int idx = this.drop_down_department_list.SelectedIndex;
            if (idx > -1)
            {                
                Guid id = new Guid(this.drop_down_department_list.Items[idx].Value);
                
                using (DataAccessLayer dc = new DataAccessLayer())
                {

                    var query = from dep in dc.DataContext.DepartmentLevels
                                where dep.DepartmentId == id
                                select dep;

                    int count = query.Count();
                    if (count > 0)
                    {
                        this.DepartmentLevelList.DataSource = query;
                        this.DepartmentLevelList.DataBind();
                    }
                    else this.DepartmentLevelList.Visible = false;                    
                }
            }
        }

        protected void delete_dep_level(object sender, EventArgs e)
        {
            Button b = (Button)sender;          
            
            using (DataAccessLayer dc = new DataAccessLayer())
            {
                DepartmentLevel dl = dc.DataContext.DepartmentLevels.SingleOrDefault(elem => elem.Id.Equals(new Guid(b.CommandArgument)));
                if (dl != null)
                {
                    dc.DataContext.DepartmentLevels.DeleteOnSubmit(dl);
                    dc.DataContext.SubmitChanges();
                }
            }
        }


        protected void delete_dep(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            using (DataAccessLayer dc = new DataAccessLayer())
            {
                Department dl = dc.DataContext.Departments.SingleOrDefault(elem => elem.Id.Equals(new Guid(b.CommandArgument)));
                ListItem li = this.drop_down_department_list.Items.FindByValue(b.CommandArgument);
                this.drop_down_department_list.Items.Remove(li);
                if (dl != null)
                {
                    dc.DataContext.DepartmentLevels.DeleteAllOnSubmit(dl.DepartmentLevels);
                    dc.DataContext.Departments.DeleteOnSubmit(dl);
                    dc.DataContext.SubmitChanges();
                }
            }
        }


        protected void UpdateLevelList(object sender, EventArgs e)
        {

        }

        protected void SaveLevel(object sender, EventArgs e)
        {
            DepartmentLevel d = new DepartmentLevel();
            d.DepartmentId = new Guid(this.drop_down_department_list.SelectedValue);
            d.Id = Guid.NewGuid();
            d.Name = this.tb_level_name.Text;
            d.Level = int.Parse(this.tb_level_level.Text);


            this.tb_level_name.Text = "";
            this.tb_level_level.Text = "";

            using (DataAccessLayer dc = new DataAccessLayer())
            {
                dc.DataContext.DepartmentLevels.InsertOnSubmit(d);
                dc.DataContext.SubmitChanges();                
            }
        }
    }
}
